<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(C卷,100分)- 内存冷热标记（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>现代计算机系统中通常存在多级的存储设备&#xff0c;针对海量 workload 的优化的一种思路是将热点内存页优先放到快速存储层级&#xff0c;这就需要对内存页进行冷热标记。</p> 
<p>一种典型的方案是基于内存页的访问频次进行标记&#xff0c;如果统计窗口内访问次数大于等于设定阈值&#xff0c;则认为是热内存页&#xff0c;否则是冷内存页。</p> 
<p>对于统计窗口内跟踪到的访存序列和阈值&#xff0c;现在需要实现基于频次的冷热标记。内存页使用页框号作为标识。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>第一行输入为 N&#xff0c;表示访存序列的记录条数&#xff0c;0 &lt; N ≤ 10000。</p> 
<p>第二行为访存序列&#xff0c;空格分隔的 N 个内存页框号&#xff0c;页面号范围 0 ~ 65535&#xff0c;同一个页框号可能重复出现&#xff0c;出现的次数即为对应框号的频次。</p> 
<p>第三行为热内存的频次阈值 T&#xff0c;正整数范围 1 ≤ T ≤ 10000。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>第一行输出标记为热内存的内存页个数&#xff0c;如果没有被标记的热内存页&#xff0c;则输出 0 。</p> 
<p>如果第一行 &gt; 0&#xff0c;则接下来按照访问频次降序输出内存页框号&#xff0c;一行一个&#xff0c;频次一样的页框号&#xff0c;页框号小的排前面。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">10<br /> 1 2 1 2 1 2 1 2 1 2<br /> 5</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">2<br /> 1<br /> 2</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">内存页1和内存页2均被访问了5次&#xff0c;达到了阈值5&#xff0c;因此热内存页有2个。内存页1和内存页2的访问频次相等&#xff0c;页框号小的排前面。</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">5<br /> 1 2 3 4 5<br /> 3</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">0</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">访存跟踪里面访存频次没有超过3的&#xff0c;因此热内存个数为0。</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>简单的多条件排序问题。</p> 
<p>具体逻辑请看代码实现。</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">const rl &#61; require(&#34;readline&#34;).createInterface({ input: process.stdin });
var iter &#61; rl[Symbol.asyncIterator]();
const readline &#61; async () &#61;&gt; (await iter.next()).value;

void (async function () {
  const n &#61; parseInt(await readline());

  const seqs &#61; (await readline()).split(&#34; &#34;).map(Number);

  const cnts &#61; {};
  for (let num of seqs) {
    cnts[num] &#61; (cnts[num] ?? 0) &#43; 1; // ?? 是高级语法&#xff0c;如果cnts[num]为null或者undefined时&#xff0c;返回0&#xff0c;实际牛客环境可能不支持
  }

  const threshold &#61; parseInt(await readline());

  // Object.entries返回的是数组&#xff0c;返回值数组的元素也是一个数组[key, value]&#xff0c;其中key,value就是对象的键值对
  const entries &#61; Object.entries(cnts).filter((a) &#61;&gt; a[1] &gt;&#61; threshold);

  console.log(entries.length);

  // entries元素数组的索引0是对象key&#xff0c;索引1是对象val
  entries
    .sort((a, b) &#61;&gt; (a[1] !&#61; b[1] ? b[1] - a[1] : a[0] - b[0]))
    .forEach((a) &#61;&gt; console.log(a[0]));
})();
</code></pre> 
<p></p> 
<h4 id="Java%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">Java算法源码</h4> 
<pre><code class="language-java">import java.util.HashMap;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int n &#61; sc.nextInt();

    HashMap&lt;Integer, Integer&gt; cnts &#61; new HashMap&lt;&gt;();
    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
      int num &#61; sc.nextInt();
      cnts.put(num, cnts.getOrDefault(num, 0) &#43; 1);
    }

    int threshold &#61; sc.nextInt();

    cnts.keySet().removeIf(num -&gt; cnts.get(num) &lt; threshold);

    System.out.println(cnts.size());

    cnts.entrySet().stream()
        .sorted(
            (a, b) -&gt;
                a.getValue() - b.getValue() !&#61; 0
                    ? b.getValue() - a.getValue()
                    : a.getKey() - b.getKey())
        .forEach(a -&gt; System.out.println(a.getKey()));
  }
}
</code></pre> 
<p></p> 
<h4 id="Python%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
n &#61; int(input())
seqs &#61; list(map(int, input().split()))
threshold &#61; int(input())

# 统计各个内存页框号出现的次数
cnts &#61; {}
for num in seqs:
    cnts.setdefault(num, 0)
    cnts[num] &#43;&#61; 1

# 过滤出现次数达到阈值的内存页&#xff0c;即热内存页数量
items &#61; list(filter(lambda x: x[1] &gt;&#61; threshold, cnts.items()))

# 打印热内存页数量
print(len(items))

# 如果存在热内存页
if len(items) &gt; 0:
    # 那么优先按照访问频次降序&#xff0c;如果访问频次相同&#xff0c;则再按页框号升序&#xff0c;items中记录元素是[页框号, 访问频次]
    items.sort(key&#61;lambda x: (-x[1], x[0]))

    for num, _ in items:
        print(num)
</code></pre> 
<p></p> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

#define MAX_SIZE 10000

typedef struct Mem {
    int num;
    int count;
} Mem;

int cmp(const void *a, const void *b) {
    Mem *A &#61; *((Mem **) a);
    Mem *B &#61; *((Mem **) b);

    if (A-&gt;count !&#61; B-&gt;count) {
        return B-&gt;count - A-&gt;count;
    } else {
        return A-&gt;num - B-&gt;num;
    }
}

int main() {
    int n;
    scanf(&#34;%d&#34;, &amp;n);

    Mem *mem[MAX_SIZE];
    int mem_size &#61; 0;

    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
        int num;
        scanf(&#34;%d&#34;, &amp;num);

        int j &#61; 0;
        for (; j &lt; mem_size; j&#43;&#43;) {
            if (mem[j]-&gt;num &#61;&#61; num) {
                mem[j]-&gt;count&#43;&#43;;
                break;
            }
        }

        if(j &lt; mem_size) continue;

        mem[mem_size] &#61; (Mem *) malloc(sizeof(Mem));
        mem[mem_size]-&gt;num &#61; num;
        mem[mem_size]-&gt;count &#61; 1;

        mem_size&#43;&#43;;
    }

    int threshold;
    scanf(&#34;%d&#34;, &amp;threshold);

    qsort(mem, mem_size, sizeof(Mem *), cmp);

    int hot_mem_size &#61; mem_size;
    for (int i &#61; 0; i &lt; mem_size; i&#43;&#43;) {
        if (mem[i]-&gt;count &lt; threshold) {
            hot_mem_size &#61; i;
            break;
        }
    }

    printf(&#34;%d\n&#34;, hot_mem_size);

    for (int i &#61; 0; i &lt; hot_mem_size; i&#43;&#43;) {
        printf(&#34;%d\n&#34;, mem[i]-&gt;num);
    }

    return 0;
}</code></pre> 
<p></p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>